package com.kolon.business.mConcise.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringEscapeUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mConcise.controller.MConciseViewController")
public class MConciseViewController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mConcise/mConciseView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url =  MsgUtil.getMessage("domain.url");

		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String type = ComUtil.nullChk(request.getParameter("type"), "TopNews"); //TopNewsList , CeoInfoList, ManageList
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
			
		//검색 관련
		String SearchTitle = request.getParameter("SearchTitle");
		String SearchFirst     = ComUtil.nullChk(request.getParameter("SearchFirst"));
		String SearchStartDate = ComUtil.nullChk(request.getParameter("SearchStartDate"),"");
		String SearchEndDate   = ComUtil.nullChk(request.getParameter("SearchEndDate"),"");
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/XMLParser/mConcise_xml.jsp?mode=").append("3");
		buffer.append("&userId=").append(id);
		buffer.append("&companyCode=").append(type);
		buffer.append("&articleCD=").append(request.getParameter("articleCD"));
		
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		Map map = new HashMap();
		String tbChk = "N";
		// XML 데이터 
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ArticleCD", el.getChild("ArticleCD").getText());			
			map.put("Title", StringEscapeUtils.unescapeXml(el.getChild("Title").getText()));
			map.put("Contents", StringEscapeUtils.unescapeXml(el.getChild("Contents").getText()).replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText());			
			map.put("Importance", el.getChild("Importance").getText());
			if (String.valueOf(map.get("Contents")).indexOf("<table") != -1 || 
					String.valueOf(map.get("Contents")).indexOf("<TABLE") != -1)
				tbChk = "Y";
		}		
		
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("page", page);
		modelAndView.addObject("type", request.getParameter("type"));
		modelAndView.addObject("tbChk", tbChk);
		
		modelAndView.addObject("SearchTitle",SearchTitle);
		modelAndView.addObject("SearchFirst",SearchFirst);
		modelAndView.addObject("SearchStartDate",SearchStartDate);
		modelAndView.addObject("SearchEndDate",SearchEndDate);
		
		return modelAndView;
	}

}
